<div class="bg-light lter b-b wrapper-md">
  <h1 class="m-n font-thin h3">频道管理</h1>
</div>
<div class="wrapper-md"  >
  <div class="panel panel-default">
    <div ng-controller="infoEditCtrl">
      <div class="row">
        <div class="col-sm-12" >
          <form name="form-contact" ng-submit="processForm()" class="form-horizontal">
            <div class="panel-heading">
              <span class="h4">
                <button class="btn btn-default btn-sm " ng-click="goBack()"><i class="fa fa-mail-reply fa-lg"></i>&nbsp; 返回</button>
              </span>
            </div>
            <div class="panel-body">            
              <div class="panel-heading">
                <span class="h4" style="color: blue;">频道简介信息</span>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">频道分类</label>
                <div class="col-sm-5">
                  <ui-select multiple ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" ng-disabled="disabled">
                    <ui-select-match placeholder="请选择所属分类">{{$item.ClassName}} &lt;{{$item.ClassRemark}}&gt;</ui-select-match>
                    <ui-select-choices  repeat="person in people | propsFilter: {ClassName: $select.search, Id: $select.search}">
                      <div ng-bind-html="person.ClassName | highlight: $select.search"></div>
                      <small>
                        编号: <span ng-bind-html="''+person.Id | highlight: $select.search"></span>
                      </small>
                    </ui-select-choices>
                  </ui-select>
                </div>
              </div>
              <div class="line line-dashed b-b line-lg pull-in"></div>
              <div class="form-group">
                <label class="col-sm-1 control-label">频道名称</label>
                <div class="col-sm-5">
                 <input type="text" class="form-control" placeholder="频道名称" ng-model="contact.ChannelName" required >
                </div>

                <label  class="col-sm-1 control-label">长名称</label>
                <div class="col-sm-5">
                <input type="text" class="form-control" placeholder="频道长名称" ng-model="contact.ChannelLongName" required >
               </div>
              </div>
            <div class="line line-dashed b-b line-lg pull-in"></div>



            <div class="form-group">
              <label class="col-sm-1 control-label">排序</label>
              <div class="col-sm-5">
                <input type="number" class="form-control"  min="0" max="99"  placeholder="排序" ng-model="contact.ChannelDisplayOrder" required >
              </div>
            </div>
            <div class="line line-dashed b-b line-lg pull-in"></div>

            <div class="form-group">
              <label class="col-sm-1 control-label">备注</label>  
              <div class="col-sm-10">
                <textarea class="form-control" ng-model="contact.ChannelRemark" rows="6" placeholder="备注"></textarea>
              </div>
            </div>
            <div class="line line-dashed b-b line-lg pull-in"></div>

            <div class="form-group">
              <label class="col-sm-1 control-label">图标:</label>  
            <div class="col-sm-5">
          <img src="" name="ims" id="imgData" onerror="javascript:this.src='img/common/300x300.png';" style="height: 90px;text-align: center;line-height: 90px;" alt="图标预览">
              <span id="browsebtn">
              <input type="file" name="File" id="File" />
              </span>
              <input type="button" value="确定" onclick="readLocalFile()"/>
            </div>
          </div>
          
            <div class="line line-dashed b-b line-lg pull-in"></div>
            <div class="panel-body">
              <div class="panel-heading">
                <span class="h4" style="color: blue;">配置信息</span>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">启用/停用</label>
                <div class="col-sm-1">
                  <label class="i-switch m-t-xs m-r">
                    <input type="checkbox" ng-model="ChannelEnable" >
                    <i></i>
                  </label>
                </div>


                <label class="col-sm-1 control-label">PC</label>
                <div class="col-sm-1">
                  <label class="i-switch m-t-xs m-r">
                    <input type="checkbox" ng-model="ChannelDisplayOnPc" >
                    <i></i>
                  </label>
                </div>
                <label class="col-sm-1 control-label">Phone</label>
                <div class="col-sm-1">
                  <label class="i-switch m-t-xs m-r">
                    <input type="checkbox" ng-model="ChannelDisplayOnPhone" >
                    <i></i>
                  </label>
                </div>

                <label class="col-sm-1 control-label">Pad</label>
                <div class="col-sm-1">
                  <label class="i-switch m-t-xs m-r">
                    <input type="checkbox"  ng-model="ChannelDisplayOnPad" >
                    <i></i>
                  </label>
                </div>

                <label class="col-sm-1 control-label">TV</label>
                <div class="col-sm-1">
                  <label class="i-switch m-t-xs m-r">
                    <input type="checkbox"  ng-model="ChannelDisplayOnTv" >
                    <i></i>
                  </label>
                </div>
              </div>
              <div class="line line-dashed b-b line-lg pull-in"></div>

              <!--隐藏字段-->
              <input type="hidden"  ng-model="contact.Id"  >
              <input type="hidden"  ng-model="contact.ChannelUuid"  >
              <input type="hidden"  ng-model="contact.AdderId"  >
              <input type="hidden"  ng-model="contact.AdderName"  >
              <input type="hidden"  ng-model="contact.AddTime"  >
              <input type="hidden"  ng-model="contact.UpdaterId"  >
              <input type="hidden"  ng-model="contact.UpdaterName"  >
              <input type="hidden"  ng-model="contact.UpdateTime"  >
              <input type="hidden"  ng-model="contact.DelFlag" ng-init="contact.DelFlag='0'"  >
              <!--隐藏字段-->


            </div>
            <footer class="panel-footer text-right bg-light lter">
              <button type="submit" class="btn btn-success">提交</button>
            </footer>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

</div>


<!--angularjs-->
<script type="text/javascript">
  app.controller('infoEditCtrl', function ($scope, $state, $stateParams,$http,$window) {

//返回按钮：开始
$scope.$window = $window;
$scope.goBack = function() {
 $state.go('app.channel.channel');
};
//返回按钮：结束

var infoId = $stateParams.infoId;
console.log('获取路由参数infoId：'+infoId);


  //如果infoId不为空，查询详情：开始
  if('' != infoId ){
    var getInfoUrl = API_URL+"channel_info?query=delflag:0,id:"+infoId+"&sortby=updateTime&order=desc";    
    $http.get(getInfoUrl).success(function (response) {

      var obj = response[0];

      obj.ChannelDisplayOrder = Number(obj.ChannelDisplayOrder);

      if("0" == obj.ChannelEnable){
        $scope.ChannelEnable = false;
      }else {
        $scope.ChannelEnable=true;
      }
      if("0" == obj.ChannelDisplayOnPad){
        $scope.ChannelDisplayOnPad = false;
      }else {
        $scope.ChannelDisplayOnPad = true;
      }
      if("0" == obj.ChannelDisplayOnPc){
        $scope.ChannelDisplayOnPc = false;
      }else {
        $scope.ChannelDisplayOnPc = true;
      }
      if("0" == obj.ChannelDisplayOnPhone){
        $scope.ChannelDisplayOnPhone = false;
      }else {
        $scope.ChannelDisplayOnPhone = true;
      }
      if("0" == obj.ChannelDisplayOnTv){
        $scope.ChannelDisplayOnTv = false;
      }else {
        $scope.ChannelDisplayOnTv = true;
      }

      $scope.contact = obj;

    });
  }
//如果infoId不为空，查询详情：结束




  //表单提交开始
  $scope.processForm= function (){

  //1.参数处理

//发起POST请求
var postInfoUrl = API_URL+"channel_info";
var formData =  $scope.contact;
  //1.参数处理
  formData.AddTime = getNowDBFormatDate();
  formData.UpdateTime = getNowDBFormatDate();
  formData.AdderId = 1;
  formData.AdderName = '1';
  formData.UpdaterId = 1;
  formData.UpdaterName = '1';
  formData.ChannelUuid='';
  formData.ChannelDisplayOrder=formData.ChannelDisplayOrder + '';


  if(true == $scope.ChannelEnable){
    formData.ChannelEnable = '1';
  }else {
    formData.ChannelEnable='0';
  }
  if(false == $scope.ChannelDisplayOnPc){
    formData.ChannelDisplayOnPc = '0';
  }else {
    formData.ChannelDisplayOnPc = '1';
  }
  if(false == $scope.ChannelDisplayOnPad){
    formData.ChannelDisplayOnPad = '0';
  }else {
     formData.ChannelDisplayOnPad = '1';
  }
  if(false == $scope.ChannelDisplayOnPhone){
    formData.ChannelDisplayOnPhone = '0';
  }else {
     formData.ChannelDisplayOnPhone = '1';
  }
  if(false == $scope.ChannelDisplayOnTv){
    formData.ChannelDisplayOnTv = '0';
  }else {
    formData.ChannelDisplayOnTv = '1';
  }
  console.log(JSON.stringify(formData));

  //修改
  if("undefined"!=typeof(formData.Id)){
    var channelId = Number(infoId);
    $.ajax({  
      async : false, //是否异步  
      cache : false, //是否使用缓存  
      type : 'put', //请求方式,post  
      dataType : "json", //数据传输格式  
      url : postInfoUrl+'/'+infoId, //请求链接  
      data: JSON.stringify(formData),
      error : function() {  
        console.log('亲，网络有点不给力呀！');  
      },  
      success : function(data) { 
              //得到了 修改人的Id
              var url =  API_URL+"r_channel_2_class";

              var objs = MY_ASYNC_AJAX(url+"?query=delflag:0,ChannelId:"+channelId);

              if(objs!=null&&objs.length>0){
                for(var i =0; i<objs.length;i++){
                  $.ajax({  
              async : false, //是否异步  
              cache : false, //是否使用缓存  
              type : 'delete', //请求方式,post  
              url : url+'/'+objs[i].Id, //请求链接  
              error : function() {  
                console.log('亲，网络有点不给力呀！');  
              },  
              success : function(data) {


              }  
            }); 

                }
              }
              var classArr =  $scope.multipleDemo.selectedPeopleWithGroupBy;

              if(''!= classArr){

                for(var i =0;i<classArr.length;i++){
                 var pushClassUrl =  API_URL+"r_channel_2_class";
                 var classFormData = {};
                 classFormData.AddTime = getNowDBFormatDate();
                 classFormData.AdderId = 1;
                 classFormData.AdderName = '1';
                 classFormData.delflag = '0';
                 classFormData.ChannelId = channelId;
                 classFormData.ClassId = classArr[i].Id;
                 $.ajax( {  
                    async : false, //是否异步  
                    cache : false, //是否使用缓存  
                    type : 'post', //请求方式,post  
                    dataType : "json", //数据传输格式  
                    url : pushClassUrl, //请求链接  
                    data: JSON.stringify(classFormData),
                    error : function() {  
                      console.log('亲，网络有点不给力呀！');  
                    },  
                    success : function(data) { 

                    }  
                  }); 

               }
             }
             top.layer.msg('修改成功', {icon: 6});

             $state.go('app.channel.channel');
           }  
         }); 
  }else{

    $.ajax({  
      async : false, //是否异步  
      cache : false, //是否使用缓存  
      type : 'post', //请求方式,post  
      dataType : "json", //数据传输格式
      url : postInfoUrl, //请求链接  
      data: JSON.stringify(formData),
      error : function() {  
        console.log('亲，网络有点不给力呀！');  
      },  
      success : function(data) {
        console.log("成功后的ID"+data[0].Id);
        var classArr =  $scope.multipleDemo.selectedPeopleWithGroupBy;
        if(''!= classArr){
          for(var i =0;i<classArr.length;i++){
            var pushClassUrl =  API_URL+"r_channel_2_class";
            var exists = MY_ASYNC_AJAX(pushClassUrl+"?query=delflag:0,ChannelId:"+data[0].Id+",ClassId:"+classArr[i].Id);
            if(null==exists){
              var classFormData = {};
              classFormData.AddTime = getNowDBFormatDate();
              classFormData.AdderId = 1;
              classFormData.AdderName = '1';
              classFormData.delflag = '0';
              classFormData.ChannelId = data[0].Id;
              classFormData.ClassId = classArr[i].Id;
              $.ajax({  
                        async : false, //是否异步  
                        cache : false, //是否使用缓存  
                        type : 'post', //请求方式,post  
                        dataType : "json", //数据传输格式  
                        url : pushClassUrl, //请求链接  
                        data: JSON.stringify(classFormData),
                        error : function() {  
                          console.log('亲，网络有点不给力呀！');  
                        },  
                        success : function(data) { 

                        }  
                      }); 

            }
          }
        }
        top.layer.msg('保存成功', {icon: 6});
        //2.成功跳转页面
        $state.go('app.channel.channel');
      }  
    }); 

  }
}
  //表单提交结束
});



app.filter('propsFilter', function() {
  return function(items, props) {
    var out = [];

    if (angular.isArray(items)) {
      items.forEach(function(item) {
        var itemMatches = false;

        var keys = Object.keys(props);
        for (var i = 0; i < keys.length; i++) {
          var prop = keys[i];
          var text = props[prop].toLowerCase();
          if (item[prop].toString().toLowerCase().indexOf(text) !== -1) {
            itemMatches = true;
            break;
          }
        }

        if (itemMatches) {
          out.push(item);
        }
      });
    } else {
          // Let the output be the input untouched
          out = items;
        }

        return out;
      };
    })







//选择类型下拉框
app.controller('SelectCtrl', function($scope, $http, $stateParams,$timeout) {



  $scope.disabled = undefined;
  $scope.searchEnabled = undefined;

  $scope.enable = function() {
    $scope.disabled = false;
  };

  $scope.disable = function() {
    $scope.disabled = true;
  };

  $scope.enableSearch = function() {
    $scope.searchEnabled = true;
  }

  $scope.disableSearch = function() {
    $scope.searchEnabled = false;
  }

  $scope.clear = function() {
    $scope.person.selected = undefined;
    $scope.address.selected = undefined;
    $scope.country.selected = undefined;
  };


  $scope.personAsync = {selected : "wladimir@email.com"};
  $scope.peopleAsync = [];


  $scope.counter = 0;
  $scope.someFunction = function (item, model){
    $scope.counter++;
    $scope.eventResult = {item: item, model: model};
  };

  $scope.removed = function (item, model) {
    $scope.lastRemoved = {
      item: item,
      model: model
    };
  };

  $scope.person = {};

  var infoUrl = API_URL+"channel_class?query=delflag:0";

  var formData =  MY_ASYNC_AJAX(infoUrl);
  $scope.people = formData;
  $scope.multipleDemo = {};
  $scope.multipleDemo.colors = ['Blue','Red'];

        //已经存在的
        var infoId = $stateParams.infoId;
        $scope.multipleDemo.selectedPeopleWithGroupBy = [];

        if(''!=infoId&&infoId!=undefined){
          var classUrl = API_URL+"r_channel_2_class?query=delflag:0,ChannelId:"+infoId;
          var classList = MY_ASYNC_AJAX(classUrl);
          console.log('获取classList：'+classList);
          if(null !=  classList){
            debugger;
            for(var i= 0;i<classList.length;i++){
              var infoClassUrl = API_URL+"channel_class?query=delflag:0,Id:"+classList[i].ClassId;
              var infoClass = MY_ASYNC_AJAX(infoClassUrl)[0];
              for(var j=0;j<formData.length;j++){
                if($scope.people[j].Id == infoClass.Id){
                  console.log('我就是你想要的数据'+$scope.people[j].Id);
                  $scope.multipleDemo.selectedPeopleWithGroupBy.push($scope.people[j]);
                  break;
                }
              }        
            }
          }
        }else{
          var classId = $stateParams.classId;
          
          if(classId!=''&&classId!=undefined){
            for(var j=0;j<formData.length;j++){
              if($scope.people[j].Id == classId){
                $scope.multipleDemo.selectedPeopleWithGroupBy.push($scope.people[j]);
                break;
              }
            }
          }else{
            alert("啥都没有");
          }
        }

      });


      </script>
      <!--angularjs-->










